package com.timber.util;

import java.sql.*;
import java.util.*;


public class ITPContractStateAction extends SLDataObjectAdaptor
{

  public static final String NEW_TRADE             = "N";
  public static final String RETURN_TRADE          = "R";

  public static final String PENDING_STATUS       = "P";
  public static final String OUTSTANDING_STATUS   = "O";

  // Contract Type
  public static final String TRADE                = "T";
  public static final String COLLATERAL           = "C";
  public static final String REPO                 = "R";
  public static final String PAY_TO_HOLD          = "P";

  public static final String CASH_COLL            = "C";
  public static final String NON_CASH_COLL        = "N";
  public static final String CASH_POOL_COLL       = "P";
  public static final String BULK_CASH_POOL_COLL  = "T";

  public static final String SETTLED_CASH         = "C";
  public static final String SETTLED_SECURITY     = "S";
  public static final String SETTLED_BOTH         = "CS";

  public static final String INVALID              = "INVALID";
  public static final String HASH_DELIM           = ":";

  public static final String US_DOMESTIC_PROCESSED_TRADE         = "Loanet";

  public static final String TRAN_TYPE        = "TranType";
  public static final String PENDING          = "Pending";
  public static final String CONTRACT_TYPE    = "ContractType";
  public static final String COLLATERAL_TYPE  = "CollateralType";
  public static final String SETTLED_STATUS   = "SettledStatus";
  public static final String STATE_DESC       = "ContractStateDesc";

  public static final String ACTION_AMEND            = "Amend";
  public static final String ACTION_CHANGE           = "Change";
  public static final String ACTION_CANCEL           = "Cancel";
  public static final String ACTION_RATE_CHANGE      = "RateChange";
  public static final String ACTION_CORP_ACTION      = "CorporateAction";
  public static final String ACTION_TRADE_SPLIT      = "TradeSplit";
  public static final String ACTION_BCP_RATE_CHANGE  = "BulkCashPoolRateChange";
  public static final String ACTION_TRADE_RETURN     = "TradeReturn";
  public static final String ACTION_INQUIRY          = "Inquiry";
  public static final String ACTION_ALLOCATE         = "Allocate";

  //NOTE: following actions are not  actual database table field - will be
  //used from the front end to differentiate:
  //         - full and partial returns
  public static final String ACTION_TRADE_FULL_RETURN = "TradeFullReturn";
  //         - Inquiry by identifiers (parent id, group id, basket id)
  public static final String ACTION_INQUIRY_BY_BASKET_ID = "InquiryBasketId";
  public static final String ACTION_INQUIRY_BY_GROUP_ID  = "InquiryGroupId";
  public static final String ACTION_INQUIRY_BY_PARENT_ID = "InquiryParentId";
  public static final String ACTION_CALL_AND_HOLD 	= "CallAndHold";
  public static final String ACTION_CHANGE_PC 		= "ChangePC";
  //         - full and partial allocations
  public static final String ACTION_FULL_ALLOCATE    = "Full Allocate";

  // For Repo trade
  public static final String ACTION_REPO_TRADE_MATURITY = "RepoTradeMaturity";
  public static final String ACTION_REPO_TRADE_ROLLOVER = "RepoTradeRollover";
  public static final String ACTION_REPO_TRADE_CHANGE_NO_ROLLOVER = "RepoTradeChangeNoRollover";
  
  /*	actions set in the database come first	*/
  public static final int ACTION_AMEND_IDX            = 0;
  public static final int ACTION_CHANGE_IDX           = 1;
  public static final int ACTION_CANCEL_IDX           = 2;
  public static final int ACTION_RATE_CHANGE_IDX      = 3;
  public static final int ACTION_CORP_ACTION_IDX      = 4;
  public static final int ACTION_TRADE_SPLIT_IDX      = 5;
  public static final int ACTION_BCP_RATE_CHANGE_IDX  = 6;
  public static final int ACTION_TRADE_RETURN_IDX     = 7;
  public static final int ACTION_INQUIRY_IDX          = 8;
  public static final int ACTION_ALLOCATE_IDX         = 9;

  public static final int ACTION_COUNT_DB		= 10;

  public static final int ACTION_TRADE_FULL_RETURN_IDX = 10;
  public static final int ACTION_INQUIRY_BY_BASKET_ID_IDX = 11;
  public static final int ACTION_INQUIRY_BY_GROUP_ID_IDX  = 12;
  public static final int ACTION_INQUIRY_BY_PARENT_ID_IDX = 13;
  public static final int ACTION_CALL_AND_HOLD_IDX 	= 14;
  public static final int ACTION_CHANGE_PC_IDX 		= 15;
  public static final int ACTION_FULL_ALLOCATE_IDX	= 16;

  public static final int ACTION_COUNT			= 17;

  // DO NOT MODIFY array ORDER - MUST BE KEPT IN SYNC WITH THE IDX values ABOVE!!!
  public static final String[] actionMap = {
    ACTION_AMEND,
    ACTION_CHANGE,
    ACTION_CANCEL,
    ACTION_RATE_CHANGE,
    ACTION_CORP_ACTION,
    ACTION_TRADE_SPLIT,
    ACTION_BCP_RATE_CHANGE,
    ACTION_TRADE_RETURN,
    ACTION_INQUIRY,
    ACTION_ALLOCATE,
    ACTION_TRADE_FULL_RETURN,
    ACTION_INQUIRY_BY_BASKET_ID,
    ACTION_INQUIRY_BY_GROUP_ID,
    ACTION_INQUIRY_BY_PARENT_ID,
    ACTION_CALL_AND_HOLD,
    ACTION_CHANGE_PC,
    ACTION_FULL_ALLOCATE
  };

  public boolean load(ResultSet rs) throws SQLException
  {
    tranType          = rs.getString( TRAN_TYPE );
    pending           = rs.getString( PENDING );
    contractType      = rs.getString( CONTRACT_TYPE );
    collateralType    = rs.getString( COLLATERAL_TYPE);
    settledStatus     = rs.getString( SETTLED_STATUS);
    stateDesc         = rs.getString( STATE_DESC );
    amend             = rs.getString( ACTION_AMEND );
    change            = rs.getString( ACTION_CHANGE);
    cancel            = rs.getString( ACTION_CANCEL );
    rateChange        = rs.getString( ACTION_RATE_CHANGE );
    corpAction        = rs.getString( ACTION_CORP_ACTION );
    tradeSplit        = rs.getString( ACTION_TRADE_SPLIT);
    bcpRateChange     = rs.getString( ACTION_BCP_RATE_CHANGE );
    tradeReturn       = rs.getString( ACTION_TRADE_RETURN );
    inquiry           = rs.getString( ACTION_INQUIRY );
    allocate          = rs.getString( ACTION_ALLOCATE );

    for( int i = 0; i < ACTION_COUNT_DB; i++ )
      actionAllowed[ i ]=
      ( "N".equals(rs.getString( actionMap[ i ] )) ? false : true );

    return true;

  }


  public String getTranType()       { return tranType;}

  public String getPending()        { return pending;}

  public String getContractType()   { return contractType;}

  public String getCollateralType() { return collateralType;}

  public String getSettledStatus()  { return settledStatus;}

  public String getStateDesc()      { return stateDesc;}
/*
  public String getAmend()          { return amend;}

  public String getChange()         { return change;}

  public String getCancel()         { return cancel;}

  public String getRateChange()     { return rateChange;}

  public String getCorpAction()     { return corpAction;}

  public String getTradeSplit()     { return tradeSplit;}

  public String getBCPRateChange()  { return bcpRateChange;}

  public String getTradeReturn()    { return tradeReturn;}

  public String getInquiry()        { return inquiry;}

  public String getAllocate()       { return allocate;}
*/

  public boolean isActionAllowed( int idx )
  {
    try
    {
      return actionAllowed[ idx ];
    }
    catch( Exception ex )
    {
      return false;
    }
  }


  public Object hashKey()
  {
      StringBuffer hk = new StringBuffer();

      hk.append( tranType);
      hk.append( HASH_DELIM);
      hk.append( pending);
      hk.append( HASH_DELIM);
      hk.append( contractType);
      hk.append( HASH_DELIM);
      hk.append( collateralType);

      if (settledStatus != null)
      {
        settledStatus = settledStatus.trim();
        if (settledStatus.length() == 0)
        {
        }
        else
        {
            hk.append( HASH_DELIM);
            hk.append( settledStatus);
        }
      }

      return   hk.toString();
  }

  public String toString()
  {
      StringBuffer printObj = new StringBuffer();

      printObj.append( TRAN_TYPE + ":" + tranType + "\n");
      printObj.append( PENDING + ":" + pending + "\n");
      printObj.append( CONTRACT_TYPE + ":" + contractType + "\n");
      printObj.append( COLLATERAL_TYPE + ":" + collateralType + "\n");
      printObj.append( SETTLED_STATUS + ":" + settledStatus + "\n");
      printObj.append( STATE_DESC + ":" + stateDesc + "\n");
      printObj.append( ACTION_AMEND + ":" + amend + "\n");
      printObj.append( ACTION_CHANGE + ":" + change + "\n");
      printObj.append( ACTION_CANCEL + ":" + cancel + "\n");
      printObj.append( ACTION_RATE_CHANGE + ":" + rateChange + "\n");
      printObj.append( ACTION_CORP_ACTION + ":" + corpAction + "\n");
      printObj.append( ACTION_TRADE_SPLIT + ":" + tradeSplit + "\n");
      printObj.append( ACTION_BCP_RATE_CHANGE + ":" + bcpRateChange + "\n");
      printObj.append( ACTION_TRADE_RETURN + ":" + tradeReturn + "\n");
      printObj.append( ACTION_INQUIRY + ":" + inquiry + "\n");
      printObj.append( ACTION_ALLOCATE + ":" + allocate + "\n");

      return   printObj.toString();

  }


  private String tranType;
  private String pending;
  private String contractType;
  private String collateralType;
  private String settledStatus;
  private String stateDesc;
  private String amend;
  private String change;
  private String cancel;
  private String rateChange;
  private String corpAction;
  private String tradeSplit;
  private String bcpRateChange;
  private String tradeReturn;
  private String inquiry;
  private String allocate;


  private boolean[] actionAllowed = new boolean[ACTION_COUNT];
}
